<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="回访项目" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回访时间" prop="revisitTime">
        <el-date-picker clearable
          v-model="queryParams.revisitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回访时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="回访姓名" prop="revisitPersonName">
        <el-input
          v-model="queryParams.revisitPersonName"
          placeholder="请输入回访人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回访类型" prop="revisitType">
        <el-select v-model="queryParams.revisitType" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.cld_revisit_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cld:revisit:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="revisitList" @selection-change="handleSelectionChange" border>
      <el-table-column label="回访项目" align="center" prop="projectName" width="250px"/>
      <el-table-column label="回访类型" align="center" prop="revisitType" width="120px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_revisit_type" :value="scope.row.revisitType" />
        </template>
      </el-table-column>
      <el-table-column label="回访时间" align="center" prop="revisitTime" width="120px"/>
      <el-table-column label="回访人姓名" align="center" prop="revisitPersonName" width="120px"/>
      <el-table-column label="回访人电话" align="center" prop="revisitPersonPhone" width="120px"/>
      <el-table-column label="回访内容" align="center" prop="revisitContent"/>
      <el-table-column label="被回访人姓名" align="center" prop="projectAccountName"  width="120px"/>
      <el-table-column label="被回访人电话" align="center" prop="projectAccountPhone"  width="120px"/>
      <el-table-column label="评价结果" align="center" prop="revisitEvaluate"  width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.revisitEvaluate == 1" type="success">满意</el-tag>
          <el-tag v-if="scope.row.revisitEvaluate == 0" type="danger">不满意</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:revisit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cld:revisit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 添加或修改回访信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="回访项目" prop="projectId">
              <el-select filterable v-model="form.projectId" placeholder="请选择" @change="handleSelectedProject">
                <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName" :value="item.projectId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回访类型" prop="revisitType">
              <el-select filterable :disabled="form.revisitType" v-model="form.revisitType" placeholder="请选择">
                <el-option v-for="dict in dict.type.cld_revisit_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="回访人" prop="revisitPersonName">
              <el-input v-model="form.revisitPersonName" :disabled="true" placeholder="请选择回访人" >
                <el-button slot="append" icon="el-icon-search" @click="handleOpenAudit()">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回访电话"  prop="revisitPersonPhone">
              <el-input v-model="form.revisitPersonPhone" :disabled="true" placeholder="请输入回访人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="回访时间" prop="revisitTime">
              <el-date-picker clearable v-model="form.revisitTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择回访时间"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被回访人" prop="projectAccountId">
              <el-select filterable v-model="form.projectAccountId" placeholder="请选择">
                <el-option v-for="item in projectAccountOptions" :key="item.projectAccountId" :label="item.userName" :value="item.projectAccountId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="回访内容">
              <el-input v-model="form.revisitContent" placeholder="请输入回访内容" type="textarea" rows="10"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="回访图片" prop="revisitImg">
              <image-upload v-model="form.revisitImg" @input="handleImageInput"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择回访人 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="selectedUserOpen" append-to-body title="选择回访人"
               width="70%">
      <selected-user v-if="selectedUserOpen" @childInput="handleSelectedUser"></selected-user>
    </el-dialog>
  </div>
</template>

<script>
import { listRevisit, getRevisit, delRevisit, addRevisit, updateRevisit } from "@/api/cld/revisit";
import selectedUser from "@/views/system/user/selectedUser.vue";
import {getList} from '@/api/cld/project'
import {getAccountListByProjectId} from '@/api/cld/account'

export default {
  name: "revisit",
  dicts: ['cld_revisit_type'],
  components: {selectedUser},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回访信息表格数据
      revisitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        revisitType: null,
        revisitTime: null,
        revisitPersonName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "回访项目不能为空", trigger: "change"}
        ],
        revisitTime: [
          {required: true, message: "回访时间不能为空", trigger: "change"}
        ],
        revisitPersonName: [
          {required: true, message: "请选择回访人", trigger: "change"}
        ],
        bRevisitPersonId: [
          {required: true, message: "请选择被回访人", trigger: "change"}
        ],
        revisitType: [
          {required: true, message: "回访类型不能为空", trigger: "change"}
        ],
      },
      // 选择回访人
      selectedUserOpen: false,
      // 项目列表
      projectOptions: [],
      // 项目账户列表
      projectAccountOptions: [],
      // 回访项目
      projectId:'',
      // 被回访人
      projectAccountId:'',
    };
  },
  created() {
    this.getList();
    this.handleGetProjectList();
  },
  methods: {
    /** 查询回访信息列表 */
    getList() {
      this.loading = true;
      listRevisit(this.queryParams).then(response => {
        this.revisitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        revisitType: null,
        revisitTime: null,
        revisitPersonId: null,
        revisitPersonName: null,
        revisitPersonPhone: null,
        revisitImg: null,
        revisitContent: null,
        bRevisitPersonId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.revisitId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回访信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const revisitId = row.revisitId || this.ids
      getRevisit(revisitId).then(response => {
        this.form = response.data;
        this.handleGetProjectAccountList(this.form.projectId);
        this.open = true;
        this.title = "修改回访信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.revisitId != null) {
            updateRevisit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRevisit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const revisitIds = row.revisitId || this.ids;
      this.$modal.confirm('是否确认删除回访信息编号为"' + revisitIds + '"的数据项？').then(function() {
        return delRevisit(revisitIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/revisit/export', {
        ...this.queryParams
      }, `revisit_${new Date().getTime()}.xlsx`)
    },
    // 图片上传成功
    handleImageInput(e) {
      this.form.revisitImg = e;
    },
    // 选择回访人
    handleOpenAudit() {
      this.selectedUserOpen = true;
    },
    // 选择回访人回调
    handleSelectedUser(e) {
      this.form.revisitPersonName = e.nickName;
      this.form.revisitPersonId = e.userId;
      this.form.revisitPersonPhone = e.phonenumber;
      this.selectedUserOpen = false;
    },
    // 查询项目列表
    handleGetProjectList() {
      getList({
        projectStatus: 2
      }).then(res => {
        this.projectOptions = res.data;
      })
    },
    // 选择项目时
    handleSelectedProject(e){
      this.handleGetProjectAccountList(this.form.projectId);
    },
    // 查询项目账户列表
    handleGetProjectAccountList(projectId) {
      getAccountListByProjectId(projectId).then(res => {
        this.projectAccountOptions = res.data;
      })
    },
  }
}
</script>
